/*
    The I Ching (the Book of Changes) plasmoid.
    Copyright (C) 2002-2009 Jean Pierre Charalambos.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "relationship.h"

void Relationship::second ( ) {
    Hexagram * mHex = new Hexagram ( refHexagram->getId() );
    int lineId;
    for ( int i=0; i<3 ;i++ ) {
        lineId = refHexagram->getInf()->getComponent ( i )->getId();
        if ( lineId == 0 )
            mHex->setLine ( i, 1 );
        if ( lineId ==3 )
            mHex->setLine ( i, 2 );
    }
    for ( int i=0; i<3 ;i++ ) {
        lineId = refHexagram->getSup()->getComponent ( i )->getId();
        if ( lineId == 0 )
            mHex->setLine ( ( i+3 ), 1 );
        if ( lineId ==3 )
            mHex->setLine ( ( i+3 ), 2 );
    }
    secondID = mHex->getId();
    delete mHex;
}

void Relationship::sequence ( ) {
    if ( refHexagram->getId() == 1 )
        sequenceID = 64;
    else
        sequenceID = refHexagram->getId() - 1;
}

void Relationship::constrast ( ) {
    Hexagram * mHex;
    Trigram * inf, *sup;
    inf = new Trigram ();
    sup = new Trigram ();

    for ( int i=0; i<3 ;i++ )
        inf->setLine ( ( 2-i ), refHexagram->getSup()->getComponent ( i )->getId() );
    for ( int i=0; i<3 ;i++ )
        sup->setLine ( ( 2-i ), refHexagram->getInf()->getComponent ( i )->getId() );

    mHex = new Hexagram ( inf, sup );
    contrastID = mHex->getId();
    delete mHex;
}

void Relationship::shadow ( ) {
    Hexagram * mHex;
    int lineId;
    Line * l[6];
    for ( int i=0; i<3 ;i++ ) {
        lineId = refHexagram->getInf()->getComponent ( i )->getId();
        l[i] = new Line ( 3 - lineId );
    }
    for ( int i=0; i<3 ;i++ ) {
        lineId = refHexagram->getSup()->getComponent ( i )->getId();
        l[i+3] = new Line ( 3 - lineId );
    }
    mHex = new Hexagram ( l[0], l[1], l[2], l[3], l[4], l[5] );
    shadowID = mHex->getId();
    delete mHex;
}

void Relationship::nuclear ( ) {
    Hexagram * mHex = new Hexagram ( refHexagram->getId() );
    int lineId;
    lineId = refHexagram->getInf()->getComponent ( 1 )->getId();
    mHex->setLine ( 0, lineId );
    lineId = refHexagram->getInf()->getComponent ( 2 )->getId();
    mHex->setLine ( 1, lineId );
    lineId = refHexagram->getSup()->getComponent ( 0 )->getId();
    mHex->setLine ( 2, lineId );
    lineId = refHexagram->getInf()->getComponent ( 2 )->getId();
    mHex->setLine ( 3, lineId );
    lineId = refHexagram->getSup()->getComponent ( 0 )->getId();
    mHex->setLine ( 4, lineId );
    lineId = refHexagram->getSup()->getComponent ( 1 )->getId();
    mHex->setLine ( 5, lineId );
    nuclearID = mHex->getId();
    delete mHex;
}

void Relationship::stepOfChange ( ) {
    stepOfChangeIDs.clear();
    Hexagram * mHex = new Hexagram ( );
    int lineId;
    int hexRefId = refHexagram->getId();
    for ( int i=0; i<3 ;i++ ) {
        mHex->setId ( hexRefId );
        lineId = refHexagram->getInf()->getComponent ( i )->getId();
        if ( lineId == 0 ) {
            mHex->setLine ( i, 1 );
            stepOfChangeIDs.append ( mHex->getId() );
        }
        if ( lineId == 3 ) {
            mHex->setLine ( i, 2 );
            stepOfChangeIDs.append ( mHex->getId() );
        }
    }
    for ( int i=0; i<3 ;i++ ) {
        mHex->setId ( hexRefId );
        lineId = refHexagram->getSup()->getComponent ( i )->getId();
        if ( lineId == 0 ) {
            mHex->setLine ( ( i+3 ), 1 );
            stepOfChangeIDs.append ( mHex->getId() );
        }
        if ( lineId == 3 ) {
            mHex->setLine ( ( i+3 ), 2 );
            stepOfChangeIDs.append ( mHex->getId() );
        }
    }
}

#include "relationship.moc"
